<?php

namespace App\Exceptions;

use Exception;
use http\Exception\BadMethodCallException;
use http\Exception\InvalidArgumentException;
use HttpException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */
    public function render($request, Exception $exception)
    {
        switch (true) {
            case $exception instanceof BaseHttpException:
                return response()->json(['msg' => $exception->getMessage(), 'state' => 'err', 'data' => $exception->getData()]);
                break;
            case $exception instanceof BusinessException:
                return response()->json(['msg' => $exception->getMessage(), 'state' => 'err', 'data' =>[],'trace'=>$exception->getTrace()],200);
                break;
            case $exception instanceof ValidationException:
                $errors = $exception->errors();
                if (!empty($errors) && !empty(current($errors)[0])) {
                    return response()->json(['msg' => current($errors)[0], 'state' => 'err','data'=>[]], 200);
                }
                break;
            case $exception instanceof AuthenticationException:
            case $exception instanceof AuthorizationException:
                return response()->json(['msg' => config('error.1006', '授权失败'), 'state' => 'err',], 401);
                break;
            case $exception instanceof ModelNotFoundException:
                return response()->json(['msg' => config('error.1004', '资源不存在'), 'state' => 'err',], 404);
                break;
            case $exception instanceof ThrottleRequestsException:
                return response()->json(['msg' => config('error.1021', '请求过于频繁'), 'state' => 'err',], $exception->getStatusCode());
                break;
            case $exception instanceof UnauthorizedHttpException:
            case $exception instanceof InvalidArgumentException:
                return response()->json(['msg' => '登录已过期，请重新登录', 'state' => 'err', 'data' => (object)[]]);
                break;
            case $exception instanceof MethodNotAllowedHttpException:
                return response()->json(['msg' => '路由请求错误！', 'state' => 'err', 'data' => (object)[]]);
                break;
            case $exception instanceof NotFoundHttpException:
                return response()->json(['msg' => '请求的资源不存在！', 'state' => 'err', 'data' => (object)[]],404);
                break;
            case $exception instanceof Exception:
                return response()->json(['msg' => $exception->getMessage(),'state' => 'err', 'data' => (object)[],'trace'=>$exception->getTraceAsString()]);
                break;

            default:
                break;
        }
        return parent::render($request, $exception);
    }
}
